package com.analyse.stock.config;

import ch.qos.logback.core.PropertyDefinerBase;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.system.SystemUtil;

import java.io.File;
import java.util.Optional;

public class LogbackConfig extends PropertyDefinerBase {
    static String STOCK_LOG = "STOCK_LOG";

    public static String getPath() {
        return SystemUtil.get(STOCK_LOG);
    }

    @Override
    public String getPropertyValue() {
        String jpomLog = SystemUtil.get(STOCK_LOG);
        return Optional.ofNullable(jpomLog).orElseGet(() -> {
            String locationPath = ClassUtil.getLocationPath(this.getClass());
            File file = FileUtil.file(FileUtil.getParent(locationPath, 2), "logs");
            String path = FileUtil.getAbsolutePath(file);
            System.out.println(path);
            SystemUtil.set(STOCK_LOG, path);
            return path;
        });
    }
}
